package org.example.librarysystemspringboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

// 标记为配置类，由 Spring 容器加载配置
@Configuration
// 启用 WebSocket 消息代理功能
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 配置消息代理
     * @param config 消息代理配置对象
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/queue"); // 启用简单的消息代理，支持 /topic 和 /queue 前缀的消息
        config.setApplicationDestinationPrefixes("/app"); // 设置应用程序的目的地址前缀
    }

    /**
     * 注册 STOMP 端点
     * @param registry STOMP 端点注册对象
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS(); // 注册 /ws 端点，允许所有源访问，并使用 SockJS 作为后备方案
    }
}